# vim:ts=4:sw=4:noet:
""" Validated input box """

import wx
from c.ui.wxw.common import loadWxImage, getProperPath
from c.base.platform import Platform

class CheckedEntry(wx.Panel):
	def __init__(self, parent, callbkCheck, callbkDone, initVal=""):
		wx.Panel.__init__(self, parent, -1)
		hbox = wx.BoxSizer(wx.HORIZONTAL)

		self.callbkDone = callbkDone
		self.callbkCheck = callbkCheck
		
		self.textctrl = wx.TextCtrl(self, -1, initVal, style = wx.TE_PROCESS_ENTER)
		self.textctrl.Bind(wx.EVT_TEXT, self.onUpdate)
		self.textctrl.Bind(wx.EVT_TEXT_ENTER, self.onEnter)
		self.textctrl.SetFocus()

		self.imGood = wx.Bitmap(getProperPath(Platform.getDataFilePath("tick.png")))
		self.imBad = wx.Bitmap(getProperPath(Platform.getDataFilePath("cross.png")))
		self.statusImage = wx.StaticBitmap(self, -1, self.imBad)
		
		hbox.Add(self.textctrl, 1, 0, 0)
		hbox.Add(self.statusImage, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 10)

		self.SetSizer(hbox)
	
	def GetValue(self):
		return self.textctrl.GetValue()

	def onUpdate(self, evt):
		txt = self.textctrl.GetValue()
		ok, result = self.callbkCheck(txt)
		if ok:
			self.statusImage.SetBitmap(self.imGood)
			self.statusImage.SetToolTip(wx.ToolTip("Method name OK"))
		else:
			self.statusImage.SetBitmap(self.imBad)
			self.statusImage.SetToolTip(wx.ToolTip(result))

	def onEnter(self, evt):
		self.callbkDone()
